

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>pyamp.web.http.headers &mdash; pyamp v1.2 documentation</title>
    <link rel="stylesheet" href="../../../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../../../_static/pygments.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../../../',
        VERSION:     '1.2',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../../../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../../../_static/doctools.js"></script>
    <link rel="top" title="pyamp v1.2 documentation" href="../../../../index.html" />
    <link rel="up" title="Module code" href="../../../index.html" /> 
  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../../../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../../../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li><a href="../../../../index.html">pyamp v1.2 documentation</a> &raquo;</li>
          <li><a href="../../../index.html" accesskey="U">Module code</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <h1>Source code for pyamp.web.http.headers</h1><div class="highlight"><pre>
<span class="c"># Copyright 2012 Brett Ponsler</span>
<span class="c"># This file is part of pyamp.</span>
<span class="c">#</span>
<span class="c"># pyamp is free software: you can redistribute it and/or modify</span>
<span class="c"># it under the terms of the GNU General Public License as published by</span>
<span class="c"># the Free Software Foundation, either version 3 of the License, or</span>
<span class="c"># (at your option) any later version.</span>
<span class="c">#</span>
<span class="c"># pyamp is distributed in the hope that it will be useful,</span>
<span class="c"># but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
<span class="c"># MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the</span>
<span class="c"># GNU General Public License for more details.</span>
<span class="c">#</span>
<span class="c"># You should have received a copy of the GNU General Public License</span>
<span class="c"># along with pyamp.  If not, see &lt;http://www.gnu.org/licenses/&gt;.</span>
<span class="sd">&#39;&#39;&#39;The headers module contains a class which provides the ability</span>
<span class="sd">to create a specific HTTP header.</span>

<span class="sd">&#39;&#39;&#39;</span>
<span class="kn">import</span> <span class="nn">re</span>

<span class="kn">from</span> <span class="nn">pyamp.web.http.constants</span> <span class="kn">import</span> <span class="n">OperatingSystems</span> <span class="k">as</span> <span class="n">OS</span>
<span class="kn">from</span> <span class="nn">pyamp.web.http.constants</span> <span class="kn">import</span> <span class="n">HeaderIds</span><span class="p">,</span> <span class="n">ContentTypes</span>


<div class="viewcode-block" id="Header"><a class="viewcode-back" href="../../../../web.html#pyamp.web.http.headers.Header">[docs]</a><span class="k">class</span> <span class="nc">Header</span><span class="p">:</span>
    <span class="sd">&#39;&#39;&#39;The Header class provides the ability to create a specific</span>
<span class="sd">    HTTP header.</span>

<span class="sd">    &#39;&#39;&#39;</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">identifier</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;</span>
<span class="sd">        * identifier -- The header identifier</span>
<span class="sd">        * value -- The value for the header</span>

<span class="sd">        &#39;&#39;&#39;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__id</span> <span class="o">=</span> <span class="n">identifier</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__value</span> <span class="o">=</span> <span class="n">value</span>

<div class="viewcode-block" id="Header.getId"><a class="viewcode-back" href="../../../../web.html#pyamp.web.http.headers.Header.getId">[docs]</a>    <span class="k">def</span> <span class="nf">getId</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;Get the ID for this header.&#39;&#39;&#39;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">__id</span>
</div>
<div class="viewcode-block" id="Header.getValue"><a class="viewcode-back" href="../../../../web.html#pyamp.web.http.headers.Header.getValue">[docs]</a>    <span class="k">def</span> <span class="nf">getValue</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;Get the value for this header.&#39;&#39;&#39;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">__value</span>

    <span class="c">##### Content type methods #####</span>
</div>
    <span class="nd">@classmethod</span>
<div class="viewcode-block" id="Header.contentType"><a class="viewcode-back" href="../../../../web.html#pyamp.web.http.headers.Header.contentType">[docs]</a>    <span class="k">def</span> <span class="nf">contentType</span><span class="p">(</span><span class="n">cls</span><span class="p">,</span> <span class="n">contentType</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;Create a content type header.</span>

<span class="sd">        * contentType -- The type of content</span>

<span class="sd">        &#39;&#39;&#39;</span>
        <span class="k">return</span> <span class="n">Header</span><span class="p">(</span><span class="n">HeaderIds</span><span class="o">.</span><span class="n">ContentType</span><span class="p">,</span> <span class="n">contentType</span><span class="p">)</span>
</div>
    <span class="nd">@classmethod</span>
<div class="viewcode-block" id="Header.textHtmlContent"><a class="viewcode-back" href="../../../../web.html#pyamp.web.http.headers.Header.textHtmlContent">[docs]</a>    <span class="k">def</span> <span class="nf">textHtmlContent</span><span class="p">(</span><span class="n">cls</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;Create an text/html content type header.&#39;&#39;&#39;</span>
        <span class="k">return</span> <span class="n">Header</span><span class="o">.</span><span class="n">contentType</span><span class="p">(</span><span class="n">ContentTypes</span><span class="o">.</span><span class="n">TextHtml</span><span class="p">)</span>
</div>
    <span class="nd">@classmethod</span>
<div class="viewcode-block" id="Header.jsonContent"><a class="viewcode-back" href="../../../../web.html#pyamp.web.http.headers.Header.jsonContent">[docs]</a>    <span class="k">def</span> <span class="nf">jsonContent</span><span class="p">(</span><span class="n">cls</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;Create a JSON content type header.&#39;&#39;&#39;</span>
        <span class="k">return</span> <span class="n">Header</span><span class="o">.</span><span class="n">contentType</span><span class="p">(</span><span class="n">ContentTypes</span><span class="o">.</span><span class="n">Json</span><span class="p">)</span>

    <span class="c">##### Location methods #####</span>
</div>
    <span class="nd">@classmethod</span>
<div class="viewcode-block" id="Header.location"><a class="viewcode-back" href="../../../../web.html#pyamp.web.http.headers.Header.location">[docs]</a>    <span class="k">def</span> <span class="nf">location</span><span class="p">(</span><span class="n">cls</span><span class="p">,</span> <span class="n">location</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;Create a location header.</span>

<span class="sd">        * location -- The location</span>

<span class="sd">        &#39;&#39;&#39;</span>
        <span class="k">return</span> <span class="n">Header</span><span class="p">(</span><span class="n">HeaderIds</span><span class="o">.</span><span class="n">Location</span><span class="p">,</span> <span class="n">location</span><span class="p">)</span>

    <span class="c">##### Methods for retreiving data from headers #####</span>
</div>
    <span class="nd">@classmethod</span>
<div class="viewcode-block" id="Header.getOperatingSystem"><a class="viewcode-back" href="../../../../web.html#pyamp.web.http.headers.Header.getOperatingSystem">[docs]</a>    <span class="k">def</span> <span class="nf">getOperatingSystem</span><span class="p">(</span><span class="n">cls</span><span class="p">,</span> <span class="n">headers</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;Get the operating system from the given headers.</span>

<span class="sd">        * headers -- The headers</span>

<span class="sd">        &#39;&#39;&#39;</span>
        <span class="n">osMatches</span> <span class="o">=</span> <span class="p">[</span>
            <span class="p">(</span><span class="n">OS</span><span class="o">.</span><span class="n">Windows3_11</span><span class="p">,</span> <span class="s">&#39;Win16&#39;</span><span class="p">),</span>
            <span class="p">(</span><span class="n">OS</span><span class="o">.</span><span class="n">Windows95</span><span class="p">,</span> <span class="s">&#39;(Windows 95)|(Win95)|(Windows_95)&#39;</span><span class="p">),</span>
            <span class="p">(</span><span class="n">OS</span><span class="o">.</span><span class="n">Windows98</span><span class="p">,</span> <span class="s">&#39;(Windows 98)|(Win98)&#39;</span><span class="p">),</span>
            <span class="p">(</span><span class="n">OS</span><span class="o">.</span><span class="n">Windows2000</span><span class="p">,</span> <span class="s">&#39;(Windows NT 5.0)|(Windows 2000)&#39;</span><span class="p">),</span>
            <span class="p">(</span><span class="n">OS</span><span class="o">.</span><span class="n">WindowsXP</span><span class="p">,</span> <span class="s">&#39;(Windows NT 5.1)|(Windows XP)&#39;</span><span class="p">),</span>
            <span class="p">(</span><span class="n">OS</span><span class="o">.</span><span class="n">WindowsServer2003</span><span class="p">,</span> <span class="s">&#39;(Windows NT 5.2)&#39;</span><span class="p">),</span>
            <span class="p">(</span><span class="n">OS</span><span class="o">.</span><span class="n">WindowsVista</span><span class="p">,</span> <span class="s">&#39;(Windows NT 6.0)&#39;</span><span class="p">),</span>
            <span class="p">(</span><span class="n">OS</span><span class="o">.</span><span class="n">Windows7</span><span class="p">,</span> <span class="s">&#39;(Windows NT 7.0)&#39;</span><span class="p">),</span>
            <span class="p">(</span><span class="n">OS</span><span class="o">.</span><span class="n">WindowsNT4</span><span class="p">,</span> <span class="s">&#39;(Windows NT 4.0)|(WinNT4.0)|(WinNT)|&#39;</span> \
                 <span class="s">&#39;(Windows NT)&#39;</span><span class="p">),</span>
            <span class="p">(</span><span class="n">OS</span><span class="o">.</span><span class="n">WindowsME</span><span class="p">,</span> <span class="s">&#39;Windows ME&#39;</span><span class="p">),</span>
            <span class="p">(</span><span class="n">OS</span><span class="o">.</span><span class="n">OpenBSD</span><span class="p">,</span> <span class="s">&#39;OpenBSD&#39;</span><span class="p">),</span>
            <span class="p">(</span><span class="n">OS</span><span class="o">.</span><span class="n">SunOS</span><span class="p">,</span> <span class="s">&#39;SunOS&#39;</span><span class="p">),</span>
            <span class="p">(</span><span class="n">OS</span><span class="o">.</span><span class="n">Linux</span><span class="p">,</span> <span class="s">&#39;(Linux)|(X11)&#39;</span><span class="p">),</span>
            <span class="p">(</span><span class="n">OS</span><span class="o">.</span><span class="n">MacOS</span><span class="p">,</span> <span class="s">&#39;(Mac_PowerPC)|(Macintosh)&#39;</span><span class="p">),</span>
            <span class="p">(</span><span class="n">OS</span><span class="o">.</span><span class="n">QNX</span><span class="p">,</span> <span class="s">&#39;QNX&#39;</span><span class="p">),</span>
            <span class="p">(</span><span class="n">OS</span><span class="o">.</span><span class="n">BeOS</span><span class="p">,</span> <span class="s">&#39;BeOS&#39;</span><span class="p">),</span>
            <span class="p">(</span><span class="n">OS</span><span class="o">.</span><span class="n">OS2</span><span class="p">,</span> <span class="s">&#39;OS/2&#39;</span><span class="p">),</span>
            <span class="p">(</span><span class="n">OS</span><span class="o">.</span><span class="n">SearchBot</span><span class="p">,</span> <span class="s">&#39;(nuhk)|(Googlebot)|(Yammybot)|(Openbot)|(Slurp)&#39;</span> \
                 <span class="s">&#39;|(MSNBot)|(Ask Jeeves/Teoma)|(ia_archiver)&#39;</span><span class="p">)</span>
            <span class="p">]</span>

        <span class="n">userAgent</span> <span class="o">=</span> <span class="n">headers</span><span class="o">.</span><span class="n">getheader</span><span class="p">(</span><span class="n">HeaderIds</span><span class="o">.</span><span class="n">UserAgent</span><span class="p">)</span>

        <span class="c"># Traverse all of the operating system types and determine if</span>
        <span class="c"># any of them are found within the user agent string</span>
        <span class="k">for</span> <span class="p">(</span><span class="n">system</span><span class="p">,</span> <span class="n">matchText</span><span class="p">)</span> <span class="ow">in</span> <span class="n">osMatches</span><span class="p">:</span>
            <span class="n">regex</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span><span class="n">matchText</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">regex</span><span class="o">.</span><span class="n">search</span><span class="p">(</span><span class="n">userAgent</span><span class="p">):</span>
                <span class="k">return</span> <span class="n">system</span></div></div>
</pre></div>

          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="../../../../search.html" method="get">
      <input type="text" name="q" size="18" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../../../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../../../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li><a href="../../../../index.html">pyamp v1.2 documentation</a> &raquo;</li>
          <li><a href="../../../index.html" >Module code</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
        &copy; Copyright 2012, Brett Ponsler.
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.0.1.
    </div>
  </body>
</html>